/**
 *  @Package: com.yidu.drug.service.impl
 *  @author: Administrator
 *  @date: 2018年8月14日 上午9:59:07
 */
package com.yidu.drug.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.DrugManagerRoleModelMapper;
import com.yidu.drug.dao.DrugRoleModelMapper;
import com.yidu.drug.dao.DrugRoleModuleModelMapper;
import com.yidu.drug.model.DrugRoleModel;
import com.yidu.drug.model.DrugRoleModuleModel;
import com.yidu.drug.service.DrugRoleService;

/**
 * 类的描述 角色service层接口实现
 * @author 祝浪
 * @date 2018年8月14日
 */
@Service
public class DrugRoleServiceImpl implements DrugRoleService {
	@Resource
	DrugManagerRoleModelMapper manRoDao;
	@Resource
	DrugRoleModelMapper roDao;
	@Resource
	DrugRoleModuleModelMapper roModDao;
	
	@Override
	public void addRole(DrugRoleModel po) {
		
		
		if(!Tools.isEmpty(po.getRoleId())){
			roDao.updateByPrimaryKeySelective(po);
		}else{
			String id = Tools.getRandomUUID();
			po.setRoleId(id);
			roDao.insertSelective(po);
		}
		String[] ids=po.getModuleIds().split(",");
		for(int i=0; i<ids.length;i++){
			DrugRoleModuleModel erm=new DrugRoleModuleModel();
			erm.setModId(ids[i]);
			String roMoid = Tools.getRandomUUID();
			erm.setRmId(roMoid);
			erm.setRoleId(po.getRoleId());
			roModDao.insertSelective(erm);
		}
		
	}
	

	@Override
	public List<DrugRoleModel> findAll(DrugRoleModel drole,Page page) {
		
		
		List<DrugRoleModel> list=roDao.findAll(drole,page);
		
		
		
		for(DrugRoleModel role:list){
			
			if(role.getIsva()==0){
				role.setIsvas("是");
			}else{
				role.setIsvas("否");
			}
			role.setOptimes(Tools.SDF_TIME.format(role.getOptime()));
			
		}
		return list;
	}
	
	@Override
	public void deleteRole(String id) {
		manRoDao.deleteByRoleId(id);
		roModDao.deleteByRoleId(id);
		roDao.deleteByPrimaryKey(id);
		
	}

	@Override
	public List<DrugRoleModuleModel> findRoleModule(String roleId) {
		
		List<DrugRoleModuleModel> list=roModDao.findRoleModule(roleId);
		
		return list;
	}

	@Override
	public void updateRole(DrugRoleModel vo) {
		
		DrugRoleModel role=new DrugRoleModel();
		
		BeanUtils.copyProperties(vo, role);
		if(!Tools.isEmpty(role.getRoleId())){
			roDao.updateByPrimaryKeySelective(role);
		}else{
			String roId = Tools.getRandomUUID();
			role.setRoleId(roId);
			roDao.insertSelective(role);
		}
		List<DrugRoleModuleModel> list=roModDao.findRoleModule(vo.getRoleId());
		for (DrugRoleModuleModel ehyRoleModuleModel : list) {
			roModDao.deleteByPrimaryKey(ehyRoleModuleModel.getRmId());
		}
		
	}
	
	
	
	@Override
	public void addRoleModule(List<DrugRoleModuleModel> list) {
		for (DrugRoleModuleModel ehyRoleModuleEntity : list) {
			String id = Tools.getRandomUUID();
			ehyRoleModuleEntity.setRmId(id);
			roModDao.insertSelective(ehyRoleModuleEntity);
		}
	}
	
	@Override
	public int findRow(DrugRoleModel po) {
		int row=roDao.findRow(po);
		return row;
	}
}
